#!/bin/bash
# KWDB SDK 完整编译脚本 - Ubuntu/Linux 环境

set -e  # 遇到错误立即退出

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

echo "======================================"
echo "  KWDB SDK 完整编译脚本"
echo "  系统: $(uname -s)"
echo "  架构: $(uname -m)"
echo "  时间: $(date)"
echo "======================================"

# 切换到项目根目录
cd /home/kvgroup/chaomei/kwdb_jx/KWDB-sdk

# 检查环境
echo ""
echo "========== 1. 检查环境 =========="

echo -n "检查 Java... "
if command -v java &> /dev/null; then
    JAVA_VERSION=$(java -version 2>&1 | head -n 1)
    echo -e "${GREEN}✓${NC} $JAVA_VERSION"
else
    echo -e "${RED}✗ Java 未安装${NC}"
    exit 1
fi

echo -n "检查 Maven... "
if command -v mvn &> /dev/null; then
    MVN_VERSION=$(mvn -version 2>&1 | head -n 1)
    echo -e "${GREEN}✓${NC} $MVN_VERSION"
else
    echo -e "${RED}✗ Maven 未安装${NC}"
    exit 1
fi

echo -n "检查 Protoc... "
if command -v protoc &> /dev/null; then
    PROTOC_VERSION=$(protoc --version)
    echo -e "${GREEN}✓${NC} $PROTOC_VERSION"
else
    echo -e "${YELLOW}⚠ protoc 未安装，可能导致编译失败${NC}"
    echo "  安装命令: sudo apt-get install -y protobuf-compiler"
    read -p "是否继续编译? (y/n) " -n 1 -r
    echo
    if [[ ! $REPLY =~ ^[Yy]$ ]]; then
        exit 1
    fi
fi

# 编译 Java SDK
echo ""
echo "========== 2. 编译 Java SDK =========="
cd java-sdk

echo "清理旧的编译文件..."
rm -rf target/

echo "开始编译 Java SDK..."
if mvn clean package -DskipTests; then
    JAR_SIZE=$(ls -lh target/kwdb-sdk-1.0.0.jar | awk '{print $5}')
    echo -e "${GREEN}✓ Java SDK 编译成功${NC}"
    echo "  文件: target/kwdb-sdk-1.0.0.jar"
    echo "  大小: $JAR_SIZE"
else
    echo -e "${RED}✗ Java SDK 编译失败${NC}"
    echo ""
    echo "常见问题排查："
    echo "  1. protoc 未安装: sudo apt-get install -y protobuf-compiler"
    echo "  2. 运行修复脚本: ./fix_protoc.sh"
    echo "  3. 查看详细日志: mvn clean compile -X"
    exit 1
fi

# 编译服务端
echo ""
echo "========== 3. 编译服务端 =========="
cd ../server

echo "清理旧的编译文件..."
rm -rf target/

echo "开始编译服务端..."
if mvn clean package -DskipTests; then
    JAR_SIZE=$(ls -lh target/kwdb-server-1.0.0.jar | awk '{print $5}')
    echo -e "${GREEN}✓ 服务端编译成功${NC}"
    echo "  文件: target/kwdb-server-1.0.0.jar"
    echo "  大小: $JAR_SIZE"
else
    echo -e "${RED}✗ 服务端编译失败${NC}"
    exit 1
fi

# 编译 C++ SDK (可选)
echo ""
echo "========== 4. 编译 C++ SDK (可选) =========="
if command -v cmake &> /dev/null; then
    cd ../cpp-sdk
    
    echo "创建构建目录..."
    mkdir -p build
    cd build
    
    echo "配置 CMake..."
    if cmake .. -DCMAKE_BUILD_TYPE=Release 2>/dev/null; then
        echo "开始编译 C++ SDK..."
        if make -j$(nproc); then
            echo -e "${GREEN}✓ C++ SDK 编译成功${NC}"
            echo "  库文件:"
            ls -lh libkwdb_sdk.* 2>/dev/null || echo "    (未找到库文件)"
            echo "  示例程序:"
            ls -lh *_example 2>/dev/null || echo "    (未找到示例程序)"
        else
            echo -e "${YELLOW}⚠ C++ SDK 编译失败 (非致命错误)${NC}"
        fi
    else
        echo -e "${YELLOW}⚠ CMake 配置失败，可能缺少 gRPC 依赖${NC}"
    fi
else
    echo -e "${YELLOW}⚠ CMake 未安装，跳过 C++ SDK 编译${NC}"
fi

# 总结
cd /home/kvgroup/chaomei/kwdb_jx/KWDB-sdk

echo ""
echo "======================================"
echo "  编译完成！"
echo "======================================"
echo ""
echo -e "${GREEN}编译产物：${NC}"

if [ -f "java-sdk/target/kwdb-sdk-1.0.0.jar" ]; then
    SIZE=$(ls -lh java-sdk/target/kwdb-sdk-1.0.0.jar | awk '{print $5}')
    echo -e "  ✓ Java SDK:  java-sdk/target/kwdb-sdk-1.0.0.jar ($SIZE)"
else
    echo -e "  ${RED}✗${NC} Java SDK 未编译"
fi

if [ -f "server/target/kwdb-server-1.0.0.jar" ]; then
    SIZE=$(ls -lh server/target/kwdb-server-1.0.0.jar | awk '{print $5}')
    echo -e "  ✓ 服务端:    server/target/kwdb-server-1.0.0.jar ($SIZE)"
else
    echo -e "  ${RED}✗${NC} 服务端未编译"
fi

if [ -f "cpp-sdk/build/libkwdb_sdk.so" ] || [ -f "cpp-sdk/build/libkwdb_sdk.a" ]; then
    echo -e "  ✓ C++ SDK:   cpp-sdk/build/libkwdb_sdk.*"
else
    echo -e "  ${YELLOW}⚠${NC} C++ SDK 未编译 (可选)"
fi

echo ""
echo -e "${GREEN}启动服务端：${NC}"
echo "  cd server"
echo "  java -jar target/kwdb-server-1.0.0.jar --port 9090"
echo ""
echo -e "${GREEN}运行测试：${NC}"
echo "  python3 quick_test.py"
echo ""

